<template>
    <!-- 添加 -->
    <div class="add-box">
        <div class="data-list">
            <el-icon>
                <Tickets />
            </el-icon>
            <span>数据列表</span>
        </div>
        <el-button type="default" @click="showAddEdit = true">添加</el-button>
    </div>
    <el-table :data="tableData" border fit>
        <el-table-column prop="id" label="编号" width="100"></el-table-column>
        <el-table-column prop="name" label="类型名称"></el-table-column>
        <el-table-column prop="attributeCount" label="属性数量" width="120"></el-table-column>
        <el-table-column prop="paramCount" label="参数数量" width="120"></el-table-column>
        <el-table-column label="设置">
            <template #default="scope" class="make">
                <el-button type="default" @click="toAttrFn(scope.row)">属性列表</el-button>
                <el-button type="default" @click="toCanshuFn(scope.row)">参数列表</el-button>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button type="default" size="small" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <ElConfigProvider :locale="zhCn">
        <el-pagination v-model:currentPage="pageNum" v-model:page-size="pageSize" background
            layout="total, sizes, prev, pager, next ,jumper" :total="total" @size-change="pageSizeChange"
            @current-change="pageNumChange" :page-sizes="[5, 10, 15]" />
    </ElConfigProvider>
    <!-- 添加、编辑dialog -->
    <el-dialog v-model="showAddEdit" title="添加属性" width="500">
        <el-form :rules="rules" :model="ruleForm" ref="ruleFormRef">
            <el-form-item label="类型名称：" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancleFn">取消</el-button>
                <el-button type="primary" @click="sureFn">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup lang='ts'>
import { Tickets } from '@element-plus/icons-vue';
import { useTable } from '@/hooks/useTable'
import { pro } from '@/api'
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElConfigProvider, ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter()
const showAddEdit = ref(false)
let ruleForm = reactive<{ name: string }>({
    name: '',
})
const ruleFormRef = ref<FormInstance>()
const params = reactive({
    pageSize: 5
})
// 分页请求
const { tableData, pageNum, pageSize, total, pageNumChange, pageSizeChange, search, } = useTable(pro.getProAttrsApi, params);
// console.log('tableData', tableData, tableData.value);

// 添加、编辑表单约束
const rules = reactive<FormRules>({
    name: [
        { required: true, message: '请输入类型名称', trigger: 'blur' },
    ],
})
let editId = ref(0)
// 编辑
const editFn = (id: number) => {
    editId.value = id
    showAddEdit.value = true
    pro.getIDProductAttributeApi(id).then(res => {
        if (res.code === 200) {
            // 浅拷贝
            Object.assign(ruleForm, res.data)
        }
    })
}
// 确定
const sureFn = () => {
    if (editId.value !== 0) {
        pro.updateProductAttributeCategoryApi({ id: editId.value, name: ruleForm.name }).then(res => {
            if (res.code === 200) {
                ElMessage.success('提交成功')
                ruleFormRef.value?.resetFields()
                // 重新请求
                // tableData.value = useTable(pro.getProAttrsApi, { pageNum: pageNum.value, pageSize: pageSize.value }).tableData
            }
        })

    } else {
        pro.CreateProductAttributeCategoryApi({ name: ruleForm.name }).then(res => {
            if (res.code === 200) {
                ElMessage.success('添加成功')
                ruleFormRef.value?.resetFields()
                // pro.getProAttrsApi({ pageNum: pageNum.value, pageSize: pageSize.value }).then(res => {
                //     if (res.code === 200) {
                //         tableData.value = res.data
                //     }
                // })
            }
        })
    }
    showAddEdit.value = false
}
// 取消
const cancleFn = () => {
    ruleFormRef.value?.resetFields()
    showAddEdit.value = false
    editId.value = 0
}
// 删除
const delFn = (id: number) => {
    ElMessageBox.confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        pro.deleteProductAttributeCategoryApi(id).then(res => {
            if (res.code === 200) {
                ElMessage.success('删除成功')
                // 重新请求，刷新页面数据，以下方法数据没响应到页面
                // useTable(pro.getProAttrsApi, { pageNum: pageNum.value, pageSize: pageSize.value })
            }
        })
    })
}
// 属性列表
const toAttrFn = (row: IattributeListModel) => {
    router.push(`/pms/productAttrList?cid=${row.id}&cname=${row.name}&type=${0}`)
}
// 参数列表
const toCanshuFn = (row: IattributeListModel) => {
    router.push(`/pms/productAttrList?cid=${row.id}&cname=${row.name}&type=${1}`)
}
</script>
<style lang="less" scoped>
.add-box {
    padding: 15px;
    display: flex;
    justify-content: space-between;
    border: 1px solid #ebeef5;
    border-radius: 5px;
    align-items: center;
    margin-bottom: 20px;

    .data-list {
        display: flex;
        align-items: center;
    }
}

.el-pagination {
    margin-top: 20px;
    justify-content: end;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>